<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Admin\Department;

class DepartmentController extends Controller
{
	public function index(Request $request) {
		if($request->isMethod('post')){
			$departModel = new Department();
			$departs = $departModel->getDataAuthDeparts(session('uid'));
			$departs = Common::Arr2Tree($departs);
			return ['code'=>1,'msg'=>'ok','data'=>$departs];
		}
		return view("admin.department.index");
	}

	public function departAdd() {
		$param = $_POST;
		unset($param['_token']);
		$res = Department::insert($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'添加成功'];
		}else{
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	public function departEdit() {
		$param = $_POST;
		unset($param['_token']);
		$res = Department::where('id',$param['id'])->update($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'编辑成功'];
		}else{
			return ['code'=>0,'msg'=>'编辑失败'];
		}
	}

	public function departDel() {
		$param = $_POST;
		unset($param['_token']);
		$res = Department::where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}
}
